package com.xdcplus.web.service.bd.impl;

import cn.hutool.core.util.StrUtil;
import com.xdcplus.mom.constants.NumberConstant;
import com.xdcplus.mom.enums.ResponseEnum;
import com.xdcplus.exceptions.XdcWebException;
import java.util.Arrays;
import cn.hutool.core.collection.CollectionUtil;

import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.web.mapper.bd.BdWetWeightAlarmConfigMapper;
import com.xdcplus.web.common.pojo.entity.bd.BdWetWeightAlarmConfig;
import com.xdcplus.web.common.pojo.dto.bd.BdWetWeightAlarmConfigDTO;
import com.xdcplus.web.common.pojo.dto.bd.BdWetWeightAlarmConfigFilterDTO;
import com.xdcplus.web.common.pojo.vo.bd.BdWetWeightAlarmConfigVO;
import com.xdcplus.web.common.pojo.query.bd.BdWetWeightAlarmConfigQuery;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.xdcplus.core.utils.BeanUtils;
import com.xdcplus.pager.vo.PageVO;
import com.xdcplus.mp.utils.PageableUtils;
import com.xdcplus.mp.utils.PropertyUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.sql.Timestamp;

import com.xdcplus.web.service.bd.BdWetWeightAlarmConfigService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;

/**
 * 丝印湿重报警推送配置(BdWetWeightAlarmConfig)表服务实现类
 *
 * @author Zhecheng.Ren
 * @since 2024-10-22 15:10:15
 */
@Slf4j
@Service("bdWetWeightAlarmConfigService")
public class BdWetWeightAlarmConfigServiceImpl extends BaseServiceImpl<BdWetWeightAlarmConfigMapper, BdWetWeightAlarmConfig, BdWetWeightAlarmConfig, BdWetWeightAlarmConfigVO> implements BdWetWeightAlarmConfigService {

    @Resource
    protected BdWetWeightAlarmConfigMapper bdWetWeightAlarmConfigMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveBdWetWeightAlarmConfig(BdWetWeightAlarmConfigDTO bdWetWeightAlarmConfigDTO) {
        Integer alarmLevel = bdWetWeightAlarmConfigDTO.getAlarmLevel();
        String receiveGroups = bdWetWeightAlarmConfigDTO.getReceiveGroups();
        String receiveUsers = bdWetWeightAlarmConfigDTO.getReceiveUsers();
        String receivePositions = bdWetWeightAlarmConfigDTO.getReceivePositions();
        if (ObjectUtil.isNull(alarmLevel)) {
            throw new XdcWebException(ResponseEnum.ERROR, "报警等级不能为空");
        }
        if (StrUtil.isNotBlank(receiveGroups) && StrUtil.isNotBlank(receiveUsers) && StrUtil.isNotBlank(receivePositions)) {
            throw new XdcWebException(ResponseEnum.ERROR, "推送群组、推送人员、推送岗位只能三选一");
        }
        BdWetWeightAlarmConfig alarmConfig = bdWetWeightAlarmConfigMapper.queryBdWetWeightAlarmConfigByAlarmLevel(alarmLevel);
        if (ObjectUtil.isNotNull(alarmConfig)) {
            throw new XdcWebException(ResponseEnum.ERROR, "当前报警等级不可重复配置");
        }
        BdWetWeightAlarmConfig bdWetWeightAlarmConfig = new BdWetWeightAlarmConfig();
        BeanUtil.copyProperties(bdWetWeightAlarmConfigDTO, bdWetWeightAlarmConfig);
        bdWetWeightAlarmConfig.setCreatedTime(new Timestamp(DateUtil.current()));

        this.save(bdWetWeightAlarmConfig);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateBdWetWeightAlarmConfig(BdWetWeightAlarmConfigDTO bdWetWeightAlarmConfigDTO) {

        Long id = bdWetWeightAlarmConfigDTO.getId();
        Integer alarmLevel = bdWetWeightAlarmConfigDTO.getAlarmLevel();
        if (ObjectUtil.isNull(alarmLevel)) {
            throw new XdcWebException(ResponseEnum.ERROR, "报警等级不能为空");
        }
        BdWetWeightAlarmConfig alarmConfig = bdWetWeightAlarmConfigMapper.queryBdWetWeightAlarmConfigByAlarmLevelAndIdNot(alarmLevel, id);

        if (ObjectUtil.isNotNull(alarmConfig)) {
            throw new XdcWebException(ResponseEnum.ERROR, "当前报警等级不可重复配置");
        }
        BdWetWeightAlarmConfig bdWetWeightAlarmConfig = this.getById(id);
        if (ObjectUtil.isNull(bdWetWeightAlarmConfig)) {
            log.error("updateBdWetWeightAlarmConfig() The BdWetWeightAlarmConfig does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }

        BeanUtil.copyProperties(bdWetWeightAlarmConfigDTO, bdWetWeightAlarmConfig);
        bdWetWeightAlarmConfig.setUpdatedUser(bdWetWeightAlarmConfigDTO.getUpdatedUser());
        bdWetWeightAlarmConfig.setUpdatedTime(new Timestamp(DateUtil.current()));

        this.updateById(bdWetWeightAlarmConfig);
    }

    @Override
    public Boolean saveOrUpdateBatch(List<BdWetWeightAlarmConfig> bdWetWeightAlarmConfigList) {

        bdWetWeightAlarmConfigList.forEach(bdWetWeightAlarmConfig -> {
            BdWetWeightAlarmConfig bdWetWeightAlarmConfigParam = new BdWetWeightAlarmConfig();
            bdWetWeightAlarmConfigParam.setId(bdWetWeightAlarmConfig.getId());
            if (ObjectUtil.isNotNull(bdWetWeightAlarmConfig.getId())) {
                bdWetWeightAlarmConfig.setId(bdWetWeightAlarmConfig.getId());
                bdWetWeightAlarmConfig.setUpdatedTime(new Timestamp(DateUtil.current()));
                LambdaUpdateWrapper<BdWetWeightAlarmConfig> lambdaUpdate = Wrappers.lambdaUpdate();
                lambdaUpdate.eq(BdWetWeightAlarmConfig::getId, bdWetWeightAlarmConfig.getId());
                update(bdWetWeightAlarmConfig, lambdaUpdate);
            } else {
                bdWetWeightAlarmConfig.setCreatedTime(new Timestamp(DateUtil.current()));
                save(bdWetWeightAlarmConfig);
            }
        });
        return true;
    }

    @Override
    public Boolean saveOrUpdateBatchByDTOList(List<BdWetWeightAlarmConfigDTO> bdWetWeightAlarmConfigDTOList) {

        List<BdWetWeightAlarmConfig> bdWetWeightAlarmConfigList = BeanUtils.copyProperties(bdWetWeightAlarmConfigDTOList, BdWetWeightAlarmConfig.class);
        return saveOrUpdateBatch(bdWetWeightAlarmConfigList);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteBdWetWeightAlarmConfigLogical(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        BdWetWeightAlarmConfig bdWetWeightAlarmConfig = this.getById(id);

        if (ObjectUtil.isNull(bdWetWeightAlarmConfig)) {
            log.error("deleteBdWetWeightAlarmConfig() The BdWetWeightAlarmConfig does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }
        this.removeById(id);
    }
    
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteBdWetWeightAlarmConfigByIds(String ids) {
        Assert.notNull(ids, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());
		List<String> idList = Arrays.asList(ids.split(","));
		if(CollectionUtil.isEmpty(idList)){
           throw new XdcWebException(ResponseEnum.ERROR);
       }
		idList.forEach(id->{
			BdWetWeightAlarmConfig bdWetWeightAlarmConfig = this.getById(id);
			if (ObjectUtil.isNull(bdWetWeightAlarmConfig)) {
				log.error("deleteBdWetWeightAlarmConfig() The BdWetWeightAlarmConfig does not exist or has been deleted");
				throw new XdcWebException(ResponseEnum.ERROR);
			}
		});

        this.removeByIds(idList);
    }

    private List<BdWetWeightAlarmConfig> queryBdWetWeightAlarmConfigList(BdWetWeightAlarmConfigFilterDTO bdWetWeightAlarmConfigFilterDTO) {
        BdWetWeightAlarmConfigQuery bdWetWeightAlarmConfigQuery = BeanUtil.copyProperties(bdWetWeightAlarmConfigFilterDTO, BdWetWeightAlarmConfigQuery.class);

        return bdWetWeightAlarmConfigMapper.queryBdWetWeightAlarmConfig(bdWetWeightAlarmConfigQuery);
    }

    @Override
    public List<BdWetWeightAlarmConfigVO> queryBdWetWeightAlarmConfigVOList(BdWetWeightAlarmConfigFilterDTO bdWetWeightAlarmConfigFilterDTO) {
        return this.objectConversion(queryBdWetWeightAlarmConfigList(bdWetWeightAlarmConfigFilterDTO));
    }

    @Override
    public PageVO<BdWetWeightAlarmConfigVO> queryBdWetWeightAlarmConfig(BdWetWeightAlarmConfigFilterDTO bdWetWeightAlarmConfigFilterDTO) {
        PageVO<BdWetWeightAlarmConfigVO> pageVO = new PageVO<>();

        if (bdWetWeightAlarmConfigFilterDTO.getCurrentPage() > NumberConstant.ZERO) {
            PageableUtils.basicPage(bdWetWeightAlarmConfigFilterDTO.getCurrentPage(), bdWetWeightAlarmConfigFilterDTO.getPageSize(),
                    bdWetWeightAlarmConfigFilterDTO.getOrderType(), bdWetWeightAlarmConfigFilterDTO.getOrderField());
        }

        List<BdWetWeightAlarmConfig> bdWetWeightAlarmConfigList = queryBdWetWeightAlarmConfigList(bdWetWeightAlarmConfigFilterDTO);

        PageInfo<BdWetWeightAlarmConfig> pageInfo = new PageInfo<>(bdWetWeightAlarmConfigList);
        PropertyUtils.copyProperties(pageInfo, pageVO, this.objectConversion(bdWetWeightAlarmConfigList));

        return pageVO;
    }

    @Override
    public BdWetWeightAlarmConfigVO queryBdWetWeightAlarmConfigById(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        return this.objectConversion(this.getById(id));
    }
}
